05. Open Photo Picker

Let’s get started with getting a photo picker appearing in our app. It should show up when we tap the photo picker button, so let’s update the code for the button’s click listener:

// ImagePickerButton shows an image picker to upload a image for a message
mPhotoPickerButton.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
       Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
       intent.setType("image/jpeg");
       intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
       startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER);
   }
});

We need an integer constant for startActivityForResult here, so we’ll also define an RC_PHOTO_PICKER constant on the top of the MainActivity class:

private static final int RC_PHOTO_PICKER =  2;    

With these four lines of code in the onClick method, a file picker will be opened to help us choose between any locally stored JPEG images that are on the device.

With that set up, the app will now open an image picker where we can choose an image to send as a message. Make sure this is working before moving on! Selecting an image at this point won’t do anything in FriendlyChat for now though. In the next part, we’ll override the onActivityResult callback to send the image to Firebase Storage.

Solution Diff: 2.01-firebase-storage-photo-picker-intent